
//登录判断
function initUsers() {
  if (!$('.username').val()) {
    return alert('请输入账号');
  }
  if (!$('.password').val()) {
    return alert('请输入密码');
  } else {
    $.ajax({
      method: 'POST',
      url: '/users/login',
      data: {
        username: $('.username').val(),
        password: $('.password').val()
      }
    })
      .done(function (res, req) {
        console.log('请求成功：', res);
        
        if (res.code == 1001) {
          alert(res.msg);
          return;
        } else if (res.code == 1002) {
          alert(res.msg)
          return;
        }
        console.log('请求成功', res.data.userId)
        // req.session.sign=true;
        logId = res.data.userId
        sessionStorage.setItem('logId', logId);
        location.href = '/pages/wf_home.html'
      })
      .fail(function (err) {
        console.log('请求失败：', err)
      });
  }
}

//用户注册
function userRegistration() {
  registerData(formatJudgment, judgingJudgment);
}

//判断账号邮箱密码是否输入
function registerData(callback1, callback2) {
  if (!$('.register-username').val()) {
    alert('请输入账号');
    return;
  } else if (!$('.register-mail').val()) {
    alert('请输入邮箱');
    return;
  } else if (!$('.register-password').val()) {
    alert('请输入密码');
    return;
  } else {
    callback1(callback2);//密码格式判断
  }
}
//账号密码格式判断
function formatJudgment(callback) {
  let userNamePat = /^[a-z0-9_-]{3,9}$/   //可以使用a-z, 0 - 9, '-', '_', 最低三位, 最高9位;
  let eMailPat = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
  let passWordPat = /^[a-z0-9_-]{6,18}$/;

  if (!(userNamePat.test($('.register-username').val()))) {
    alert('用户名格式不正确')
    return;
  } else if (!(eMailPat.test($('.register-mail').val()))) {
    alert('邮箱格式不正确')
    return;
  } else if (!(passWordPat.test($('.register-password').val()))) {
    alert('密码格式不正确')
    return;
  } else {
    callback()//数据库查找是否占用
  }
}
//判断 邮箱 和 用户名 是否被占用
function judgingJudgment() {
  $.ajax({
    method: 'POSt',
    url: '/users/occupy',
    data: {
      username: $('.register-username').val(),
      userpass: $('.register-password').val(),
      useremail: $('.register-mail').val()
    }
  })
    .done(function (res) {

      console.log('成功收到数据', res.data)

      logId = res.data.userId;
      console.log('请求成功：', res);
      //将最新的登录id储存在本地缓存里
      sessionStorage.setItem('logId', logId);

      location.href = 'wf_home.html';
    })
    .fail(function (err) {
      console.log('请求失败：', err)
    });
}